package utility;

import java.io.Serializable;
import java.text.DecimalFormat;

public class Vector implements Serializable {

	private static final long serialVersionUID = 7289667406296903196L;

	public static final DecimalFormat df = new DecimalFormat("0.00");

	public double x, y;

	public Vector(double x, double y) {
		set(x, y);
	}

	public Vector() {
		x = 0;
		y = 0;
	}

	public double length() {
		return Math.sqrt(x * x + y * y);
	}

	public Vector normalize() {
		double l = length();
		return new Vector(x / l, y / l);
	}

	public void setPolar(double angle, double length) {
		x = Math.cos(angle) * length;
		y = Math.sin(angle) * length;
	}

	public void set(double x, double y) {
		this.x = x;
		this.y = y;
	}

	public void set(Vector v) {
		this.x = v.x;
		this.y = v.y;
	}

	public Vector add(Vector arg) {
		return new Vector(x + arg.x, y + arg.y);
	}

	public Vector subtract(Vector arg) {
		return new Vector(x - arg.x, y - arg.y);
	}

	public Vector multiply(double factor) {
		return new Vector(x * factor, y * factor);
	}

	public double distance(Vector v) {
		double dx = x - v.x, dy = y - v.y;
		return Math.sqrt(dx * dx + dy * dy);
	}

	public String toString() {
		return "[" + df.format(x) + ";" + df.format(y) + "]";
	}

	public static int round(double d) {
		return (int) (d + 0.5);
	}

}
